<?php
namespace Slides\Controller;
use Common\Controller\AdminBase;
class SlidesController extends AdminBase {
    public function index() {
        $termsid = I('get.termsid', 0, 'intval');
        $where = array();
        if ($termsid > 0) {
            $where['termsid'] = array("EQ", $termsid);
        }
        $db = M("Slides");
        $count = $db->where($where)->count();
        $page = $this->page($count, 20);
        $data = $db->where($where)->limit($page->firstRow . ',' . $page->listRows)->order(array("id" => "DESC"))->select();
        $Termsdb = M("Terms");
        $Terms = $Termsdb->where(array("module" => "Slides"))->getField("id,name");
        $this->assign("Terms", $Terms);
        $this->assign("Page", $page->show('Admin'));
        $this->assign("data", $data);
        $this->display();
    }
    //添加 
    public function add() {
        if (IS_POST) {
            $db = D("Slides/Slides");
            if ($db->create()) {
                $id = $db->add();
                if ($id) {
                    //更新附件状态
                    if (!empty($_POST['image'])) {
                        service("Attachment")->api_update('', 'Slides-' . $id, 1);
                    }
                    if (!empty($_POST['images'])) {
                        service("Attachment")->api_update('', 'Slides-' . $data['id'], 1);
                    }
                    $this->success("添加成功！", U("Slides/Slides/index"));
                } else {
                    $this->error("添加失败！");
                }
            } else {
                $this->error($db->getError());
            }
        } else {
            $this->display();
        }
    }
    //编辑
    public function edit() {
        if (IS_POST) {
            $db = D("Slides/Slides");
            $data = $db->create();
            if ($data) {
                if ($db->save() !== false) {
                    //更新附件状态
                    if (!empty($_POST['image'])) {
                        service("Attachment")->api_update('', 'Slides-' . $data['id'], 1);
                    }
                    if (!empty($_POST['images'])) {
                        service("Attachment")->api_update('', 'Slides-' . $data['id'], 1);
                    }
                    $this->success("更新成功！", U("Slides/Slides/index"));
                } else {
                    $this->error("更新失败！");
                }
            } else {
                $this->error($db->getError());
            }
        } else {
            $id = I('get.id', 0, 'intval');
            $data = M("Slides")->where(array("id" => $id))->find();
            if (!$data) {
                $this->error("该信息不存在！");
            }
            $this->assign($data);
            $this->display();
        }
    }
    //删除 
    public function delete() {
        if (IS_POST) {
            $ids = I('post.ids', '', '');
            $db = M("Slides");
            if (is_array($ids)) {
                foreach ($ids as $id) {
                    $db->where(array("id" => $id))->delete();
                    service("Attachment")->api_delete('Slides-' . $id);
                }
            }
            $this->success("删除成功！");
        } else {
            $id = I('get.id', 0, 'intval');
            $db = M("Slides");
            $status = $db->where(array("id" => $id))->delete();
            if ($status) {
                //删除附件
                service("Attachment")->api_delete('Slides-' . $id);
                $this->success("删除成功！");
            } else {
                $this->error("删除失败！");
            }
        }
    }
}
